package cn.iocoder.yudao.module.jikong.controller.admin.jkunit;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jikong.controller.admin.jkunit.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkunit.JkUnitDO;
import cn.iocoder.yudao.module.jikong.service.jkunit.JkUnitService;

@Tag(name = "管理后台 - 样本工具管理")
@RestController
@RequestMapping("/jikong/jk-unit")
@Validated
public class JkUnitController {

    @Resource
    private JkUnitService jkUnitService;

    @PostMapping("/create")
    @Operation(summary = "创建样本工具管理")
    @PreAuthorize("@ss.hasPermission('jikong:jk-unit:create')")
    public CommonResult<Long> createJkUnit(@Valid @RequestBody JkUnitSaveReqVO createReqVO) {
        return success(jkUnitService.createJkUnit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样本工具管理")
    @PreAuthorize("@ss.hasPermission('jikong:jk-unit:update')")
    public CommonResult<Boolean> updateJkUnit(@Valid @RequestBody JkUnitSaveReqVO updateReqVO) {
        jkUnitService.updateJkUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样本工具管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:jk-unit:delete')")
    public CommonResult<Boolean> deleteJkUnit(@RequestParam("id") Long id) {
        jkUnitService.deleteJkUnit(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样本工具管理")
                @PreAuthorize("@ss.hasPermission('jikong:jk-unit:delete')")
    public CommonResult<Boolean> deleteJkUnitList(@RequestParam("ids") List<Long> ids) {
        jkUnitService.deleteJkUnitListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样本工具管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:jk-unit:query')")
    public CommonResult<JkUnitRespVO> getJkUnit(@RequestParam("id") Long id) {
        JkUnitDO jkUnit = jkUnitService.getJkUnit(id);
        return success(BeanUtils.toBean(jkUnit, JkUnitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样本工具管理分页")
    @PreAuthorize("@ss.hasPermission('jikong:jk-unit:query')")
    public CommonResult<PageResult<JkUnitRespVO>> getJkUnitPage(@Valid JkUnitPageReqVO pageReqVO) {
        PageResult<JkUnitDO> pageResult = jkUnitService.getJkUnitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkUnitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样本工具管理 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:jk-unit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJkUnitExcel(@Valid JkUnitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JkUnitDO> list = jkUnitService.getJkUnitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样本工具管理.xls", "数据", JkUnitRespVO.class,
                        BeanUtils.toBean(list, JkUnitRespVO.class));
    }

}